import Common from "../common/Common";
import ViewBase from "./ViewBase";
import UIManager from "../manager/UIManager";
import Define from "../common/Define";
import GameManager from "../manager/GameManager";
import UserInfo from "../UserInfo";
import WXHelper from "../common/WXHelper";
import ParticleEffectManager from "../manager/ParticleEffectManager";
import HttpManager from "../manager/HttpManager";
import MiniGameInfo from "../MiniGameInfo";
import FrameAnimation from "../common/FrameAnimation";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class MainView extends ViewBase {

    @property(cc.Label)
    textGold: cc.Label = null;

    @property(cc.Node)
    btn_ranking: cc.Node = null;

    @property(cc.Node)
    btn_share: cc.Node = null;

    @property(cc.Node)
    btn_bg: cc.Node = null;

    @property(cc.Node)
    btn_sound: cc.Node = null;

    @property(cc.Node)
    btn_cannon: cc.Node = null;

    @property(cc.Node)
    btn_scene: cc.Node = null;

    @property(cc.Node)
    btn_shaman: cc.Node = null;

    @property(cc.Node)
    btn_brate: cc.Node = null;

    @property(cc.Node)
    nodeCenterGuide: cc.Node = null;

    @property(cc.Node)
    nodeCenterTitle: cc.Node = null;

    @property(cc.Node)
    nodeCannonTip: cc.Node = null;


    @property(cc.Node)
    nodeGoldTextParent: cc.Node = null;

    @property(cc.Label)
    textGuide: cc.Label = null;

    @property(cc.Node)
    node_title1: cc.Node = null;

    @property(cc.Node)
    node_title2: cc.Node = null;
    
    //btns -------- 结束 
    @property(cc.Node)
    nodeHand:cc.Node = null; // -123  130

    private nodeGoldTextParentInitPos:cc.Vec2 = null;


   
    //刷新UI 
    refreshView(isFristRefresh:boolean = false){
        this.refreshGoldCound();
        //清空上次的动作
        this.nodeHand.stopAllActions();
        this.textGuide.node.stopAllActions();
        this.textGuide.node.rotation = 0;
        this.nodeHand.position = new cc.Vec2(-123,this.nodeHand.position.y);

        this.node_title1.active = false;
        this.node_title2.active = false;

        if(Define.gameId == "001"){
            this.node_title1.active = true;
        }else if(Define.gameId == "002"){
            this.node_title2.active = true;
        }
        
        this.nodeCenterGuide.stopAllActions()
        this.nodeCenterGuide.position = new cc.Vec2(-640,0)
        this.nodeCenterGuide.runAction(cc.sequence(cc.moveTo(0.8,new cc.Vec2(0,0)).easing(cc.easeBackOut()),
            cc.callFunc(function(){
                this.handMove();
            }.bind(this))
        ));

        if(Common.isShowGoldFlyEffect){
            ParticleEffectManager.getInstance().showParticleEffect(4,cc.Vec2.ZERO,2,null,this.nodeCenterTitle);
            Common.isShowGoldFlyEffect = false;
        }

        this.changeBrateState();
        this.executeEvent()
    }

    //点击点击事件
    addEvent(){
        Common.addClickEvent(this.btn_ranking,this.onClick.bind(this));
        Common.addClickEvent(this.btn_share,this.onClick.bind(this));
        Common.addClickEvent(this.btn_bg,this.onClick.bind(this),false);
        Common.addClickEvent(this.btn_sound,this.onClick.bind(this));
        Common.addClickEvent(this.btn_cannon,this.onClick.bind(this));
        Common.addClickEvent(this.btn_brate,this.onClick.bind(this));
        Common.addClickEvent(this.btn_shaman,this.onClick.bind(this));
        Common.addClickEvent(this.btn_scene,this.onClick.bind(this));
        
    
        this.nodeGoldTextParentInitPos = this.nodeGoldTextParent.position;
        this.chnageSoundState();        


        let frame:FrameAnimation = this.btn_shaman.children[0].getComponent<FrameAnimation>(FrameAnimation)
        frame.init();
        frame.Play(8000)
    
        Common.actionLeftRightRotate(this.btn_shaman.children[0],0.125,2,10)
        Common.actionLeftRightRotate(this.btn_ranking,0.15,18,3)
        Common.actionLeftRightRotate(this.btn_share,0.2,15,5)
    }

    handMove(){
        this.nodeHand.stopAllActions();
        let actionRight = cc.moveTo(1.5,new cc.Vec2(130,this.nodeHand.position.y));
        let actionLeft = cc.moveTo(1.5,new cc.Vec2(-123,this.nodeHand.position.y));
        this.nodeHand.runAction(cc.repeatForever(cc.sequence(actionRight,actionLeft)));
        Common.actionLeftRightRotate(this.textGuide.node);
    }


    onClick(tag:string){
        if(tag == "btn_bg"){
            if(Common.pauseServer){
                return
            }
            UIManager.getInstance().hideView(Define.viewMain);
            UIManager.getInstance().hideView(Define.viewUpgrade);
            UIManager.getInstance().showView(Define.viewBattle,function(){
                Common.isPlaying = true;
                GameManager.instance.restartGame();
            });
        }else if(tag == "btn_sound"){
            this.chnageSoundState(true)
            UserInfo.clearAllDatas();
        }
        else if(tag == "btn_ranking"){
            UIManager.getInstance().showView(Define.viewRanking);
            UIManager.getInstance().hideView(Define.viewMain)
            UIManager.getInstance().hideView(Define.viewUpgrade)
        }else if(tag == "btn_share"){
            WXHelper.instance.shareAppMessage("点击分享");
            console.log(" 点击了分享按键")
        }else if(tag == "btn_cannon"){
            UIManager.getInstance().showView(Define.viewSelectCannon);
            UIManager.getInstance().hideView(Define.viewMain)
            UIManager.getInstance().hideView(Define.viewUpgrade)
        }else if(tag == "btn_brate"){
            this.changeBrateState(true);
        }
        else if(tag == "btn_shaman"){
            WXHelper.jumpToMiniProgram("wx841bc8aa850bd773");
        }else if(tag == "btn_scene"){
            UIManager.getInstance().showView(Define.viewSelectScene);
            UIManager.getInstance().hideView(Define.viewMain)
            UIManager.getInstance().hideView(Define.viewUpgrade)
        }
    }

    changeBrateState(isChange:boolean = false){
        if(isChange){
            if(UserInfo.brate == 1){
                UserInfo.brate = 0
            }else{
                UserInfo.brate = 1
            }
            UserInfo.saveBrateSate()
        }

        console.log("UserInfo.brate ========= " +UserInfo.brate);
        this.btn_brate.children[0].active = false
        this.btn_brate.children[1].active = false
        this.btn_brate.children[UserInfo.brate].active = true
    }

    chnageSoundState(isChange:boolean = false){
        if(isChange){
            Common.isOpenSound = !Common.isOpenSound
        }
        this.btn_sound.children[0].active = !Common.isOpenSound
        this.btn_sound.children[1].active = Common.isOpenSound
    }
    //刷新金币数量
    refreshGoldCound(){;
        this.textGold.string = Common.getShowNumber(UserInfo.curGold);
        let x = 60*this.textGold.string.length*-1-36;
        this.textGold.node.children[0].position = new cc.Vec2(x,-24);
        x = this.nodeGoldTextParentInitPos.x + (this.textGold.string.length - 1)*24;
        this.nodeGoldTextParent.position = new cc.Vec2(x,this.nodeGoldTextParentInitPos.y);
    }

    refreshGoldInfo(){
        this.refreshGoldCound();
    }

    executeEvent(eventTag:string = "defualt"){
        
        if(UserInfo.isHaveNewCannon()){
            this.nodeCannonTip.active = true;
            this.nodeCannonTip.stopAllActions();
            Common.actionLeftRightRotate(this.nodeCannonTip,0.15,18,3)
        }else{
            this.nodeCannonTip.active = false;
        }


        if(UserInfo.isHaveNewScene()){
            this.btn_scene.children[0].active = true;
            this.btn_scene.children[0].stopAllActions();
            Common.actionLeftRightRotate(this.btn_scene.children[0],0.15,18,3)
        }else{
            this.btn_scene.children[0].active = false;
        }
    }
}
